

#include "GiantFlamethrower.h"
#include "gamemanager.h"
#include "gamedatamanager.h"
#include "CCSkeletalSprite.h"
#include "flame.h"
#include "log.h"
#include "messagequeue.h"
#include "gamescene.h"

#define __super         Unit

GiantFlamethrower::GiantFlamethrower() {

}

GiantFlamethrower::~GiantFlamethrower() {

}

void GiantFlamethrower::init(UnitInfo const & info, float unitScale) {
	__super::init(info, unitScale);

	attribute.splashCases = 6;
}

void GiantFlamethrower::update(float ticks) {
	__super::update(ticks);
}

void GiantFlamethrower::onBulletHit(Bullet *bullet) {
	Unit::onBulletHit(bullet);
}

void GiantFlamethrower::handleStateBirth(float ticks){
	transformState(UnitState::idle);
}

void GiantFlamethrower::handleStateAim(float ticks){

}

void GiantFlamethrower::handleStateAttack(float ticks){
	if (sprite->animationIsDone()) {
		transformState(UnitState::reload);
	}
}

void GiantFlamethrower::handleStateDying(float ticks){
	if (sprite->animationIsDone()) {
		transformState(UnitState::died);
	}
}

void GiantFlamethrower::handleStateIdle(float ticks){
	if (gameScene->battleState == BattleState::standby) {
		return;
	}

	if (_timeElapsed >= attribute.reloadTime) {
		if(findDistantOpponent()) {
			transformState(UnitState::attack);
		}
	}
}

void GiantFlamethrower::handleStateReload(float ticks){
	if (sprite->animationIsDone()) {
		transformState(UnitState::idle);
	}
}

void GiantFlamethrower::handleStateWalk(float ticks){
	if (gameScene->battleState == BattleState::standby) {
		transformState(UnitState::idle);
	} else {
		move(ticks);

		if (arrivedTargetPosition) {
			transformState(UnitState::idle);
		} else if (_timeElapsed >= attribute.reloadTime) {
			if(target) {
				transformState(UnitState::walk_attack);
			}
		}
	}
}

void GiantFlamethrower::handleStateWalkAim(float ticks){

}

void GiantFlamethrower::handleStateWalkAttack(float ticks){
	if (sprite->animationIsDone()) {
		transformState(UnitState::walk_reload);
	}
}

void GiantFlamethrower::handleStateWalkReload(float ticks){
	if (sprite->animationIsDone()) {
		transformState(UnitState::walk);
	}
}

void GiantFlamethrower::onAttack() {
	if (target) {
		FlameBullet *bullet = new FlameBullet(target, &attribute);
		bullet->init();
		GM->gameScene->addBullet(bullet);
	}

	_timeElapsed = 0;
}
